﻿using System.Collections.Specialized;
using NUnit.Framework;

namespace CodeContrib.Tests
{
	public class CollectionExtensionsTests
	{
		[Test]
		public void Join_With_Complete_items()
		{
			// Arrange
			const string expected = "key1=value1&key2=value2";

			var collection = new NameValueCollection
				                 {
					                 {
						                 "key1", "value1"
					                 },
					                 {
						                 "key2", "value2"
					                 }
				                 };

			// Act
			var actual = collection.Join();

			// Assert
			Assert.AreEqual(expected, actual);
		}

		[Test]
		public void Join_With_Partial_items()
		{
			// Arrange
			const string expected = "key1=&key2=";

			var collection = new NameValueCollection();
			collection.Add("key1", null);
			collection.Add("key2", string.Empty);

			// Act
			var actual = collection.Join();

			// Assert
			Assert.AreEqual(expected, actual);
		}

		[Test]
		public void Joinitems_With_Complete_items()
		{
			// Arrange
			NameValueCollection expected = null;

			expected = new NameValueCollection();
			expected.Add("key1", "value1");
			expected.Add("key2", "value2");

			var items = new[]
				            {
					            "key1", "value1", "key2", "value2"
				            };

			// Act
			var actual = items.ToNameValueCollection();

			// Assert
			Assert.AreEqual(expected.Count, actual.Count);

			for (var i = 0; i < expected.Count; i++)
			{
				Assert.AreEqual(expected[i], actual[i]);
			}
		}

		[Test]
		public void Joinitems_With_Partial_items()
		{
			// Arrange
			var expected = new NameValueCollection
				               {
					               {
						               "key1", null
					               },
					               {
						               "key2", string.Empty
					               }
				               };

			var items = new[]
				            {
					            "key1", null, "key2", string.Empty, "key3"
				            };

			// Act
			var actual = items.ToNameValueCollection();

			// Assert
			Assert.AreEqual(expected.Count, actual.Count);

			for (var i = 0; i < expected.Count; i++)
			{
				Assert.AreEqual(expected[i], actual[i]);
			}
		}
	}
}